//
//  MXHomeDetialFootView.m
//  CatchBear
//
//  Created by 王启颖 on 2018/11/23.
//  Copyright © 2018年 王启颖. All rights reserved.
//

#import "MXHomeDetialFootView.h"
#import "MXAuthController.h"

@interface MXHomeDetialFootView ()
@property (weak, nonatomic) IBOutlet UIImageView *userHeader_image;
@property (weak, nonatomic) IBOutlet UILabel *userNickname_lbl;
@property (weak, nonatomic) IBOutlet UILabel *messageAgreeNum_lbl;
@property (weak, nonatomic) IBOutlet UILabel *messageCommentNum_lbl;
@property (weak, nonatomic) IBOutlet UIButton *messageAgree_btn;
@property (weak, nonatomic) IBOutlet UIButton *messageComment_btn;
@property (weak, nonatomic) IBOutlet UILabel *messageInfo_lbl;

@property (strong, nonatomic) NSMutableDictionary *agree_dicParams;
@end
@implementation MXHomeDetialFootView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self initCustomerControls];
}

- (void)initCustomerControls {
   
}

-(void)setModel:(MXHomePageModel *)model{
    _model = model;
    
    [self.userHeader_image changeCornerRadius:self.userHeader_image.width / 2];
    [self.userHeader_image sd_setImageWithURL:[NSURL URLWithString:model.user.user_headimg] placeholderImage:[UIImage imageNamed:@"photoAndVideoDefault"]];
    self.userNickname_lbl.text = model.user.user_nickname;
    self.messageAgreeNum_lbl.text = [NSString stringWithFormat:@"%ld赞",(long)model.messages_agreenum];
    self.messageCommentNum_lbl.text = [NSString stringWithFormat:@"%ld评论",(long)model.messages_commentnum];
    self.messageAgree_btn.selected = model.is_agree;
    self.messageInfo_lbl.text = model.messages_info;
}

- (IBAction)agreeClick:(UIButton *)sender {
    [[MXAccount sharedMXAccount] LoadTokenFromSandbox];
    NSString *token = [MXAccount sharedMXAccount].token;
    if (![token length]) {
        MXAuthController *authVC = [[MXAuthController alloc]init];
        [self.navigationController pushViewController:authVC animated:true];
        return;
    }
    
    
    sender.selected = !sender.selected;
    [self messageAgreeRequset];
}

- (IBAction)commentClick:(id)sender {
    [[MXAccount sharedMXAccount] LoadTokenFromSandbox];
    NSString *token = [MXAccount sharedMXAccount].token;
    if (![token length]) {
        MXAuthController *authVC = [[MXAuthController alloc]init];
        [self.navigationController pushViewController:authVC animated:true];
        return;
    }
    
    self.commentsBlock(self.model);
}

- (NSMutableDictionary *)agree_dicParams {
    if (!_agree_dicParams) {
        _agree_dicParams = [NSMutableDictionary dictionary];
    }
    [self initagree_dicParams];
    return _agree_dicParams;
}
- (void)initagree_dicParams {
    _agree_dicParams[@"user_id"] = [MXUserMessageTool message].user_id;
    _agree_dicParams[@"messages_id"] = self.model.messages_id;
}

#pragma mark - 网络请求 -
-(void)messageAgreeRequset{
    [self.messageAgree_btn setEnabled:NO];
    [[MXHttpManagerClass HttpRequstManager] PostURL:messageAgree params:self.agree_dicParams isHearderparam:YES success:^(NSInteger code, id result) {
        [self.messageAgree_btn setEnabled:YES];
        if (code == 0) {
            NSLog(@"%@",result);
            self.model.is_agree = !self.model.is_agree;
            if (self.model.is_agree) {
                self.model.messages_agreenum = self.model.messages_agreenum + 1;
            }else{
                self.model.messages_agreenum = self.model.messages_agreenum - 1;
            }
            self.messageAgreeNum_lbl.text = [NSString stringWithFormat:@"%ld赞",(long)self.model.messages_agreenum];
            self.agreesBlock(self.model);
        }else{
            [SVProgressHUD showImage:NULL_IMAGE status:@"参数错误"];
        }
        
    } fail:^(NSError *error) {
        [self.messageAgree_btn setEnabled:YES];
    }];
}

+ (instancetype)initMXHomeDetialFootView {
    return NSbunleloadNibName(@"MXHomeDetialFootView");
}

@end
